package com.example.demo.auth;

import com.example.demo.dto.UserDTO;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MyUserDetailService implements UserDetailsService {
    @Autowired
    UserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String s) {
        List<UserDTO> userDTOS = userMapper.selectUserByUserName(s);
        System.out.println("验证传入用户名=>"+s);
        if (userDTOS.size()>0){
            UserDTO userDTO = userDTOS.get(0);
            List<GrantedAuthority> authorities= AuthorityUtils.createAuthorityList("ROLE_ACTIVITI_"+userDTO.getRoleName(),"GROUP_"+userDTO.getRoleName());
            User user = new User(userDTO.getUsername(), userDTO.getPassword(), authorities);
            return user;
        }else{
            return null;
        }
    }
}
